﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Dracham.MapEditor.Textures;
using Dracham.MapEditor.Maps;
using System.Diagnostics;

namespace Dracham.MapEditor
{
    public partial class frmLoader : Form
    {
        private Stopwatch stopwatch;
        public frmLoader()
        {
            InitializeComponent();

            instance = this;
            stopwatch = new Stopwatch();
        }

        private static frmLoader instance;
        private void frmLoader_Load(object sender, EventArgs e)
        {
            Thread loadingThread = new Thread(LoadData);
            loadingThread.Start();

            stopwatch.Start();
        }

        public delegate void SetStatusTextCallback(String text);
        public static void SetStatusText(String text)
        {
            if (instance.InvokeRequired)
            {
                SetStatusTextCallback d = new SetStatusTextCallback(SetStatusText);
                instance.Invoke(d, text);
            }
            else
            {
                instance.lblStatusInfo.Text = text + "\r\n(" + instance.stopwatch.Elapsed.ToString() + " elapsed)";
            }
        }

        private void LoadDataDone()
        {
            SetStatusText("Okey, done...");

            this.DialogResult = DialogResult.OK;
            Application.DoEvents();
        }

        private void LoadData()
        {
            SetStatusText("Starting load...");
            String appPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\";

            SetStatusText("Loading textures...");
            Singleton.DrachamMapEditor.TextureManager = new TextureManager(appPath);

            SetStatusText("Loading zones...");
            Singleton.DrachamMapEditor.ZoneManager = new ZoneManager(appPath);

            LoadDataDone();
        }
    }
}
